package logobj.config;

import logobj.utils.BigFileUtils;

import java.io.File;
import java.util.ResourceBundle;

public class ConfProperties {
    public static String ftp_username;
    public static String ftp_pwd;
    public static String ftp_ip;
    public static int ftp_port;
    public static String ftp_path;
    public static String ftp_localpath;
    public static String trunca_tetable_status;
    public static String trunca_tetable_name;
    public static long min_file_size;
    public static String ftp_localpath_index;
    public static String ftp_localpath_sublog;
    public static int file_incision_value;
    public static int file_incision_interval_time;
    public static int file_insertdb_interval_time;
    public static int update_rownum;
    public static String file_incision_model;
    public static int file_load_max_mem;
    public static int data_warehousing_shz_maxrow;
    //数据库配置
    public static String dbtype;
    public static String  dbip;
    public static String  dbport;
    public static String  dbuser;
    public static String   dbpwd;
    public static String   driver;
    public static String   url;
    public static String  dbname;
    public static String file_runbefore_del;
    public static String file_runafter_del;
    public static String no_ftp_model;
    public static int  data_insert_maxrow;
    public static  void initConfParam(){
        min_file_size =  Long.valueOf(ResourceBundle.getBundle("conf").getString("min_file_size"));
        ftp_username = ResourceBundle.getBundle("conf").getString("ftp_username");
        ftp_pwd = ResourceBundle.getBundle("conf").getString("ftp_pwd");
        ftp_ip = ResourceBundle.getBundle("conf").getString("ftp_ip");
        ftp_port =Integer.valueOf(ResourceBundle.getBundle("conf").getString("ftp_port")) ;
        ftp_path = ResourceBundle.getBundle("conf").getString("ftp_path");
        ftp_localpath = ResourceBundle.getBundle("conf").getString("ftp_localpath");
        trunca_tetable_name = ResourceBundle.getBundle("conf").getString("trunca_tetable_name");
        trunca_tetable_status = ResourceBundle.getBundle("conf").getString("trunca_tetable_status");
        file_incision_value =Integer.valueOf(ResourceBundle.getBundle("conf").getString("file_incision_value"));
        file_incision_interval_time =Integer.valueOf(ResourceBundle.getBundle("conf").getString("file_incision_interval_time"));
        file_insertdb_interval_time =Integer.valueOf(ResourceBundle.getBundle("conf").getString("file_insertdb_interval_time"));
        file_load_max_mem =Integer.valueOf(ResourceBundle.getBundle("conf").getString("file_load_max_mem"));
        file_incision_model=ResourceBundle.getBundle("conf").getString("file_incision_model");
        data_warehousing_shz_maxrow = Integer.valueOf(ResourceBundle.getBundle("conf").getString("data_warehousing_shz_maxrow"));
        update_rownum =Integer.valueOf(ResourceBundle.getBundle("conf").getString("update_rownum"));
        file_runbefore_del=ResourceBundle.getBundle("conf").getString("file_runbefore_del");
        file_runafter_del=ResourceBundle.getBundle("conf").getString("file_runafter_del");
        no_ftp_model=ResourceBundle.getBundle("conf").getString("no_ftp_model");
        data_insert_maxrow = Integer.valueOf(ResourceBundle.getBundle("conf").getString("data_insert_maxrow"));
        if("Y".equals(file_runbefore_del.toUpperCase())){
            BigFileUtils bigFileUtils = new BigFileUtils();
            bigFileUtils.deleteDir(ftp_localpath);
            bigFileUtils = null;
        }
        choseDBType();
        createDir();
    }

    /***
     * 选择数据库类型
     * */
    private static void choseDBType(){
        dbtype = ResourceBundle.getBundle("conf").getString("dbtype");
        dbip = ResourceBundle.getBundle("conf").getString("dbip");
        dbport = ResourceBundle.getBundle("conf").getString("dbport");
        dbuser = ResourceBundle.getBundle("conf").getString("dbuser");
        dbpwd = ResourceBundle.getBundle("conf").getString("dbpwd");
        dbname = ResourceBundle.getBundle("conf").getString("dbname");
        if("gp".equals(dbtype)){
            driver = "org.postgresql.Driver";
            url = "jdbc:postgresql://"+dbip+":"+dbport+"/"+dbname+"?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true";
        }else if("m5".equals(dbtype) || "m8".equals(dbtype)){
            driver = "com.mysql.cj.jdbc.Driver";
            url = "jdbc:mysql://"+dbip+":"+dbport+"/"+dbname+"?characterEncoding=utf8&useUnicode=true&useSSL=false";
            System.out.println("url:"+url);
        }
    }


    /***
     * 初始化创建目录
     * */
    private static void createDir(){
        ftp_localpath_index = ftp_localpath +"/index";
        ftp_localpath_sublog = ftp_localpath +"/sub_log";
        File ftpdir = new File(ftp_localpath);
        File index = new File(ftp_localpath_index);
        File sublog = new File(ftp_localpath_sublog);
        if  (!ftpdir .exists()  && !ftpdir .isDirectory()) {
            ftpdir .mkdir();
        }
        if  (!index .exists()  && !index .isDirectory()) {
            index .mkdir();
        }
        if  (!sublog .exists()  && !sublog .isDirectory()) {
            sublog .mkdir();
        }
    }


}
